package com.open.party.cloud.view.home.jiCengDangJian

import android.os.Bundle
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusRecycleViewTitleBaseActivity
import com.sinothk.android.utils.inters.OnSuperListener
import com.open.party.cloud.R
import com.open.party.cloud.model.TimeBean
import com.open.party.cloud.view.home.jiCengDangJian.adapter.TimeListAdapter
import com.open.party.cloud.viewModel.ZiYuanZheViewModel
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_shadow.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.*

class TimePlanListActivity : AppEventBusRecycleViewTitleBaseActivity<TimeBean>(), OnSuperListener<TimeBean> {

    var activityId = ""

    var viewModel: ZiYuanZheViewModel? = null
    private var adapter: TimeListAdapter? = null

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip_shadow

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider_none
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("会议时间安排")

        activityId = intent.getStringExtra("activityId") as String

        viewModel = ZiYuanZheViewModel()
        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = TimeListAdapter(this)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)

        loadingRecyclerView.setLoadingMoreEnabled(false)

        refreshData()
        loadingTipView.showLoading("努力加载中")

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }
    }

    override fun loadData(pageVo: PageVo<TimeBean>) {
        viewModel!!.getTimeListData(activityId)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<ArrayList<TimeBean>>) {
        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null && result.data.size > 0) {
                    adapter!!.setData(result.data)
                    loadingTipView.showContent()
                } else {
                    loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }

        stopLoading(loadingRecyclerView, loadType)
    }

    override fun onClick(position: Int, itemData: TimeBean, flag: String?) {
        intent.putExtra("value", itemData)
        setResult(200, intent)
        finish()
    }
}